<?php
$orderServices = new OrdersService();
$orderDetailTemp = new OrderDetailstempService();
$articleService = new ArticleService();
$paymentService = new PaymentServices();
$fromEmail = $glb['contactEmail'];
$host = Tool::decode($smtp['host'], "sTunnInVietnAm@2012#");
$port = Tool::decode($smtp['port'], "sTunnInVietnAm@2012#");
$user = Tool::decode($smtp['user'], "sTunnInVietnAm@2012#");
$pass = Tool::decode($smtp['pass'], "sTunnInVietnAm@2012#");
$emailService = new EmailService($host, $port, $user, $pass);
$emailService->setSiteURL($glb['websiteURL']);
switch ($action) {
	case "confirmPI":
		if($permit == true)
		{
			if(isset($_GET['id']) && isset($_GET['fid']))
			{
				echo "<pre>";
				$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['id'], $_GET['fid']);
				$orderDetailArr = $order->getOrderDetail();
				foreach($orderDetailArr as $orderDetail)
				{
					
					$orderDetailDomain = $orderDetail->getOrderdetailsDomain();
					if($orderDetailDomain->getStt() == 1)
					{
						$orderDetailID = $orderDetailDomain->getID();					
						$orderDetailDomain->setStt(3);
					}
					if($orderDetailDomain->getStt() == 2)
					{
						$orderDetailID = $orderDetailDomain->getID();
						
						$orderDetailTempDomain = $orderDetailTemp->getOrderDetailTempByID($context, $result, $orderDetailID);
						$description = $orderDetailTempDomain->getDescription();
						$width = $orderDetailTempDomain->getWidth();
						$height = $orderDetailTempDomain->getHeight();
						$depth = $orderDetailTempDomain->getDepth();
						$cbm = $orderDetailTempDomain->getCbm();
						$priceFact = $orderDetailTempDomain->getPriceFact();
						
						$article = 	$articleService->getArticleByOid($context, $result, $orderDetailDomain->getArticleID());
						$article->getArticleDomain()->setDescription($description);
						$article->getArticleDomain()->setWidth($width);
						$article->getArticleDomain()->setHeight($height);
						$article->getArticleDomain()->setDepth($depth);
						$article->getArticleDomain()->setCbm($cbm);
						if($orderDetailDomain->getCurrency() == "USD")
							$article->getArticleDomain()->setInPriceUSD($priceFact);
						else
							$article->getArticleDomain()->setInPriceEUR($priceFact);
						
						$articleService->updateArticle($context, $result, $article->getArticleDomain());
						
						$orderDetailDomain->setPriceFact($priceFact);
						$orderDetailDomain->setAmountFact($orderDetailDomain->getQuantity() * $orderDetailDomain->getPriceFact());
						$orderDetailDomain->setCbm($cbm);
						$orderDetailDomain->setStt(3);
					}
				}
				$orderServices->updateFullOrder($context, $result, $order);
				
				$complete = $orderServices->isCompletePI($context, $result, $_GET['id']);
				if($complete)
				{
					$order->getOrdersDomain()->setType(3);
					$orderServices->updateOrder($context, $result, $order);
					// Gửi Mail và Send PI cho Customer					
					$orderFull = $orderServices->renewOrder($context, $result, $_GET['id']);
					$paymentService->createPaymentByOrder($context, $result, $orderFull);
					$reportService = new ReportService();
					$reportService->setSiteURL($glb['websiteURL']);
					$file = $reportService->generatePDFFile($context, $result, $orderFull);
					$fileName = $order->getOrdersDomain()->getOrderNumber().'.pdf';
					$PIID = $orderFull->getOrdersDomain()->getOrderID();
					$toEmail = $orderFull->getConsignee()->getCustomerDomain()->getEmail();
					$emailService->sendEmailProformaInvoiceToCustomer($context, $result, $fromEmail, $toEmail, $PIID, $fileName, $file);					
				}
				else 
				{
					$orderServices->renewOrder($context, $result, $_GET['id']);	
				}			
				$orderDetailTemp->deleteByOrderIDByFactoryID($context, $result, $_GET['id'], $_GET['fid']);
				header("Location:index.php?func=pi&opt=PIList");
			}
			header("Location:index.php?func=pi&opt=PIList");
		}
		else
			header("Location:index.php?func=dash");
	break;
	
	case "confirmPayment":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			if(isset($_GET['id']) && isset($_POST['invoiceDate']) && isset($_POST['invoiceNumber']))
			{
				$order = $orderServices->getOrderByOid($context, $result, $_GET['id']);
				$order->getOrdersDomain()->setInvoiceDate($data['invoiceDate']);
				$order->getOrdersDomain()->setInvoiceNumber($data['invoiceNumber']);
				$order->getOrdersDomain()->setType(7);
				$orderServices->updateOrder($context, $result, $order);
				header("Location:index.php?func=order&opt=listOrderF");
			}
			header("Location:index.php?func=order&opt=listOrderF");
		}
		else
		header("Location:index.php?func=dash");
	break;
			
	default:
		header("Location:index.php?func=dash");
		break;
}
?>